const EventBus = require("../template")

function testEventBusEventManagement() {
  const eventBus = new EventBus()

  console.log(
    "1. 事件优先级测试:",
    (() => {
      let order = []
      eventBus.on('priority', () => order.push('first'), { priority: 1 })
      eventBus.on('priority', () => order.push('second'), { priority: 2 })
      eventBus.on('priority', () => order.push('third'), { priority: 3 })
      eventBus.emit('priority')
      return JSON.stringify(order) === JSON.stringify(['first', 'second', 'third'])
    })()
  )

  console.log(
    "2. 事件命名空间测试:",
    (() => {
      let count = 0
      eventBus.on('user:login', () => count++)
      eventBus.on('user:logout', () => count++)
      eventBus.on('admin:login', () => count++)
      eventBus.emit('user:login')
      eventBus.emit('user:logout')
      eventBus.emit('admin:login')
      return count === 3
    })()
  )

  console.log(
    "3. 事件通配符测试:",
    (() => {
      let count = 0
      eventBus.on('user:*', () => count++)
      eventBus.emit('user:login')
      eventBus.emit('user:logout')
      eventBus.emit('admin:login')
      return count === 2
    })()
  )

  console.log(
    "4. 事件条件订阅测试:",
    (() => {
      let count = 0
      eventBus.on('conditional', () => count++, { condition: (data) => data > 5 })
      eventBus.emit('conditional', 3)
      eventBus.emit('conditional', 7)
      eventBus.emit('conditional', 10)
      return count === 2
    })()
  )

  console.log(
    "5. 事件重试机制测试:",
    (() => {
      let attempts = 0
      eventBus.on('retry', () => {
        attempts++
        if (attempts < 3) {
          throw new Error('Retry needed')
        }
      }, { retries: 3 })
      
      try {
        eventBus.emit('retry')
        return attempts === 3
      } catch (error) {
        return false
      }
    })()
  )

  console.log(
    "6. 事件超时测试:",
    (() => {
      let completed = false
      eventBus.on('timeout', () => {
        setTimeout(() => {
          completed = true
        }, 100)
      }, { timeout: 50 })
      
      eventBus.emit('timeout')
      return completed === false
    })()
  )

  console.log(
    "7. 事件链式调用测试:",
    (() => {
      let chain = []
      eventBus.on('chain1', () => {
        chain.push('chain1')
        eventBus.emit('chain2')
      })
      eventBus.on('chain2', () => {
        chain.push('chain2')
        eventBus.emit('chain3')
      })
      eventBus.on('chain3', () => {
        chain.push('chain3')
      })
      
      eventBus.emit('chain1')
      return JSON.stringify(chain) === JSON.stringify(['chain1', 'chain2', 'chain3'])
    })()
  )

  console.log(
    "8. 事件批量处理测试:",
    (() => {
      let processed = []
      eventBus.on('batch', (data) => {
        processed.push(data)
      })
      
      const batch = [1, 2, 3, 4, 5]
      batch.forEach(item => eventBus.emit('batch', item))
      
      return JSON.stringify(processed) === JSON.stringify(batch)
    })()
  )

  console.log(
    "9. 事件防抖测试:",
    (() => {
      let count = 0
      eventBus.on('debounce', () => count++, { debounce: 100 })
      
      eventBus.emit('debounce')
      eventBus.emit('debounce')
      eventBus.emit('debounce')
      
      setTimeout(() => {
        return count === 1
      }, 150)
      
      return true
    })()
  )

  console.log(
    "10. 事件节流测试:",
    (() => {
      let count = 0
      eventBus.on('throttle', () => count++, { throttle: 100 })
      
      eventBus.emit('throttle')
      eventBus.emit('throttle')
      eventBus.emit('throttle')
      
      setTimeout(() => {
        return count === 1
      }, 50)
      
      return true
    })()
  )

  console.log(
    "11. this上下文事件管理测试:",
    (() => {
      const eventMgrObj = {
        multiplier: 2,
        processEvent: function(data) {
          const bus = new EventBus()
          bus.on('process', (val) => {
            return val * this.multiplier
          })
          return bus.emit('process', data)
        }
      }
      return eventMgrObj.processEvent(5) === 10
    })()
  )

  console.log(
    "12. 复杂对象this绑定事件管理测试:",
    (() => {
      const eventMgrConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(data) {
          const bus = new EventBus()
          bus.on('process', (val) => {
            return val >= this.config.threshold
          })
          return bus.emit('process', data)
        }
      }
      return eventMgrConfigObj.processWithConfig(5) === true
    })()
  )
}

testEventBusEventManagement()
